﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using VanTaiBLL;
using VanTaiData;
using VanTaiProperty;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using Roles;
namespace VanTaiTMS.Page
{
    public partial class lap_hop_dong : System.Web.UI.Page
    {
        #region[Define]
        private DataTable _dtDichVuBocXep;
        private DataTable dtContract;
        private DataTable dtContractDetail;
        private DataTable dtContracType;
        private DataTable dtVehicle;
        private DataTable dtVehicleDetail;
        private DataTable dtCustomer;

        private DataTable dtFilter;
        private DataTable dtSearch;
        protected string SuggestionList;
        protected string StrAlert = "";
        private DataTable dtAutoComplete;
        private static bool flag = false;

        private readonly TMServicesFOB_BusinessLogic _clsServices = new TMServicesFOB_BusinessLogic();
        private clsTMContractBusiness clsContract = new clsTMContractBusiness();
        private clsTMContract_TypeBusiness clsContractType = new clsTMContract_TypeBusiness();
        private clsTMVehicleBusiness clsVehicle = new clsTMVehicleBusiness();
        private clsTMCustomersBusiness clsCustomer = new clsTMCustomersBusiness();
        #endregion

        #region[Controller]
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckPermisstion();
            Autocomplate();
            if (!IsPostBack)
            {
                BindGridContract();
                BindGridDropdownlist();
            }
        }
        protected void cmdAddNew_Click(object sender, EventArgs e)
        {
            flag = true;
            lbThongBaoLoi.Text = "";
            txtMaHopDong.Enabled = true;

            drChooseService.Enabled = false;
            txtCarryImportPrice.ReadOnly = true;
            txtCarryImportPriceHoliday.ReadOnly = true;
            txtCarryImportPriceNight.ReadOnly = true;

            txtCarryExportPrice.ReadOnly = true;
            txtCarryExportPriceNight.ReadOnly = true;
            txtCarryExportPriceHoliday.ReadOnly = true;

            txtOtherPrice.ReadOnly = true;

            ClearTextBox();
            OpenDialog("#laphopdong");
            imgSave.ToolTip = "Thêm mới";
            upNewUpdatePanel.Update();
            imgSave.Enabled = true;
        }
        protected void cmdEdit_Click(object sender, EventArgs e)
        {
            txtMaHopDong.Enabled = false;
            for (int i = 0; i < ListViewAll.Items.Count; i++)
            {
                CheckBox chkView = (CheckBox)ListViewAll.Items[i].FindControl("chkList");
                Label lbTrangThai = (Label)ListViewAll.Items[i].FindControl("lbDuyet");
                if (chkView != null)
                {
                    if (chkView.Checked)
                    {
                        imgSave.Enabled = true;
                        BindGridContractDetail(Convert.ToInt32(ListViewAll.DataKeys[i].Value.ToString()));
                        OpenDialog("#laphopdong");
                        upNewUpdatePanel.Update();
                        imgSave.ToolTip = "Cập nhật";
                        flag = false;
                    }
                }
            }
        }
        protected void cmdDel_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < ListViewAll.Items.Count; i++)
                {
                    CheckBox chkHide = (CheckBox)ListViewAll.Items[i].FindControl("chkList");
                    Label lbTrangThai = (Label)ListViewAll.Items[i].FindControl("lbDuyet");
                    if (chkHide != null)
                    {
                        if (chkHide.Checked)
                        {
                            VanTaiBLL.clsTMContractBusiness.DeleteTMContract(int.Parse(ListViewAll.DataKeys[i].Value.ToString()));
                        }
                    }
                }
            }
            catch
            {
                OpenDialog("#thongbao");
                lbThongBaoLoi.Text = "Mẫu tin còn tồn tại !";
                updatePanelThongBao.Update();
            }
        }
        protected void cmdSerach_Click(object sender, EventArgs e)
        {
            if (txtSearch.Text == "")
            {
                StrAlert += "<div class='alert alert-warning'>";
                StrAlert += "<a href='#' class='close' data-dismiss='alert'>&times;</a>";
                StrAlert += "Vui lòng nhập vào từ khoá để tìm kiếm !";
                StrAlert += "</div>";
            }
            else
            {
                Search(txtSearch.Text);
            }
        }
        protected void cmdRefesh_Click(object sender, EventArgs e)
        {
            txtSearch.Text = "";
            txtTuNgayNgayNhapKho.Text = "";
            txtDenNgayNhapKho.Text = "";
            BindGridContract();
        }
        protected void cmdExport_Click(object sender, EventArgs e)
        {

        }
        protected void cmdFilter_Click(object sender, EventArgs e)
        {
            //Fillter(txtTuNgayNgayNhapKho.Text, txtDenNgayNhapKho.Text);
        }
        //------------Button-Popup-------------------
        protected void imgSave_Click(object sender, ImageClickEventArgs e)
        {
            if (txtMaHopDong.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào mã hợp đồng !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (drChonLoaiHopDong.SelectedValue.ToString() == "-1")
            {
                lbThongBaoLoi.Text = "Vui lòng chọn loại hợp đồng !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (drKhachHang.SelectedValue.ToString() == "-1")
            {
                lbThongBaoLoi.Text = "Vui lòng chọn khách hàng !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (drSoXe.SelectedValue.ToString() == "-1")
            {
                lbThongBaoLoi.Text = "Vui lòng chọn số xe !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (txtNgayKyHopDong.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào ngày ký hợp đồng !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (txtNgayHetHan.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào ngày hết hạn hợp đồng !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (txtTienDatCoc.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào tiền đặt cọc !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (txtMaHopDong.Text != "" && drChonLoaiHopDong.SelectedValue.ToString() != "-1"
            && drKhachHang.SelectedValue.ToString() != "-1" && drSoXe.SelectedValue.ToString() != "-1"
            && txtNgayKyHopDong.Text != "" && txtTienDatCoc.Text != "")
            {
                Insert();
            }
        }
        protected void imgrefesh_Click(object sender, ImageClickEventArgs e)
        {

        }
        protected void cmdAgree_Click(object sender, EventArgs e)
        {

        }
        protected void cmdClose_Click(object sender, EventArgs e)
        {
            CloseDialog("#thongbaoxoa");
        }
        protected void cmdCloseThongBao_Click(object sender, EventArgs e)
        {
            CloseDialog("#thongbao");
        }
        //----------List-View--------------------
        protected void ListViewAll_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            try
            {
                txtMaHopDong.Enabled = false;
                HttpCookie cookie = Request.Cookies["UserLogin"];
                bool Edit = SecurityServices.HasPermisstion(StringContent.QuanLyHopDong, int.Parse(cookie["RolesID"]), TypeaAudit.Edit);
                lbThongBaoLoi.Text = "";
                if (e.CommandName == "Accept")
                {
                    if (Edit == false)
                    {
                        imgSave.Enabled = false;
                        string SenderId = ((HtmlInputHidden)e.Item.FindControl("hiddenId")).Value;
                        BindGridContractDetail(int.Parse(SenderId));
                        OpenDialog("#laphopdong");
                        upNewUpdatePanel.Update();
                        imgSave.ToolTip = "Cập nhật";
                        flag = false;
                    }
                    else
                    {
                        imgSave.Enabled = true;
                        string SenderId = ((HtmlInputHidden)e.Item.FindControl("hiddenId")).Value;
                        BindGridContractDetail(int.Parse(SenderId));
                        OpenDialog("#laphopdong");
                        upNewUpdatePanel.Update();
                        imgSave.ToolTip = "Cập nhật";
                        flag = false;
                    }
                }
            }
            catch (Exception ex)
            {
                lbThongBaoLoi.Text = ex.ToString();
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
        }
        protected void ListViewAll_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                LinkButton linkListName = (LinkButton)e.Item.FindControl("linkListName");
                LinkButton linkDatetimeExpires = (LinkButton)e.Item.FindControl("LinkButton6");
                string _jsDouble1 = ClientScript.GetPostBackClientHyperlink(linkListName, "");


                linkListName.Attributes["ondblclick"] = _jsDouble1;
                linkListName.Attributes["onclick"] = "return false;";
                //----------------------------------------
                if (DateTime.Parse(linkDatetimeExpires.Text) == DateTime.Today)
                {
                    HtmlTableRow cell = (HtmlTableRow)e.Item.FindControl("SetColor");
                    cell.BgColor = "Red";
                    cell.Attributes["class"] = "rowError";
                }
            }
        }
        //----------Data-Pager--------------------
        protected void DataPagerListAll_PreRender(object sender, EventArgs e)
        {
            DataPagerListAll.PageSize = 17;
            BindGridContract();
        }     
        //----------Dropdownlist-------------------
        protected void drSoXe_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (drSoXe.SelectedValue.ToString() == "-1")
            {
                lbSoXe.Text = string.Empty;
            }
            else
            {
                BindGridVehicleDetail(int.Parse(drSoXe.SelectedValue.ToString()));
            }
        }
        protected void drChonLoaiHopDong_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (drChonLoaiHopDong.SelectedValue.ToString() == "-1")
            {
                drChooseService.SelectedIndex = -1;

                txtCarryImportPrice.Text = "0";
                txtCarryImportPriceHoliday.Text = "0";
                txtCarryImportPriceNight.Text = "0";

                txtCarryExportPrice.Text = "0";
                txtCarryExportPriceNight.Text = "0";
                txtCarryExportPriceHoliday.Text = "0";

                txtOtherPrice.Text = "0";

                drChooseService.Enabled = false;
                txtCarryImportPrice.ReadOnly = true;
                txtCarryImportPriceHoliday.ReadOnly = true;
                txtCarryImportPriceNight.ReadOnly = true;

                txtCarryExportPrice.ReadOnly = true;
                txtCarryExportPriceNight.ReadOnly = true;
                txtCarryExportPriceHoliday.ReadOnly = true;

                txtOtherPrice.ReadOnly = true;

            }
            else
            {
                BindGridService(int.Parse(drChonLoaiHopDong.SelectedValue.ToString()));
            }
        }
        #endregion

        #region[Function]
        protected void BindGridContract()
        {
            dtContract = clsContract.Search(txtSearch.Text);
            dtFilter = clsContract.TMContract_Fillter_Date(txtTuNgayNgayNhapKho.Text, txtDenNgayNhapKho.Text);
            if (txtTuNgayNgayNhapKho.Text == "" && txtDenNgayNhapKho.Text == "")
            {
                if (dtContract != null && dtContract.Rows.Count > 0)
                {
                    ListViewAll.DataSource = dtContract;
                    ListViewAll.DataBind();
                    ListViewAll.Visible = true;
                    DataPagerListAll.Visible = true;
                }
                else
                {
                    ListViewAll.Visible = false;
                    DataPagerListAll.Visible = false;
                }
            }
            else
            {
                if (dtFilter != null && dtFilter.Rows.Count > 0)
                {
                    ListViewAll.DataSource = dtFilter;
                    ListViewAll.DataBind();
                    ListViewAll.Visible = true;
                    DataPagerListAll.Visible = true;
                }
            }
        }
        protected void BindGridContractDetail(int id)
        {
            dtContractDetail = clsContract.BindGirdTMContract_Detail_Edit(id);
            if (dtContractDetail != null && dtContractDetail.Rows.Count > 0)
            {
                lbID.Text = dtContractDetail.Rows[0]["Contract_ID"].ToString();
                lbIDXoa.Text = dtContractDetail.Rows[0]["Contract_ID"].ToString();
                txtMaHopDong.Text = dtContractDetail.Rows[0]["Contrac_Alias"].ToString();
                drChonLoaiHopDong.SelectedValue = dtContractDetail.Rows[0]["Type_Of_Contract_ID"].ToString();
                drKhachHang.SelectedValue = dtContractDetail.Rows[0]["Customers_ID"].ToString();
                drSoXe.SelectedValue = dtContractDetail.Rows[0]["Vehicle_ID"].ToString();
                txtNgayKyHopDong.Text = String.Format("{0:dd/MM/yyyy}", dtContractDetail.Rows[0]["Contract_Signed_Date"].ToString());
                txtNgayHetHan.Text = String.Format("{0:dd/MM/yyyy}", dtContractDetail.Rows[0]["Contract_Expires_Date"].ToString());
                txtTienDatCoc.Text = String.Format("{0:0,00}", dtContractDetail.Rows[0]["Contract_Payment"]).Replace(".", ",");
                txtNoiDungHopDong.Text = dtContractDetail.Rows[0]["Contract_Content"].ToString();
                txtGhiChu.Text = dtContractDetail.Rows[0]["Contract_Note"].ToString();

                if (dtContractDetail.Rows[0]["ServicesFOB_ID"] == DBNull.Value || dtContractDetail.Rows[0]["ServicesFOB_ID"].ToString() == "0")
                {
                    drChooseService.Items.Clear();
                    drChooseService.Items.Add(new ListItem("--Chọn dịch vụ--", "-1"));
                    txtCarryImportPrice.Text = "0";
                    txtCarryImportPriceHoliday.Text = "0";
                    txtCarryImportPriceNight.Text = "0";

                    txtCarryExportPrice.Text = "0";
                    txtCarryExportPriceNight.Text = "0";
                    txtCarryExportPriceHoliday.Text = "0";

                    txtOtherPrice.Text = "0";

                    drChooseService.Enabled = false;
                    txtCarryImportPrice.ReadOnly = true;
                    txtCarryImportPriceHoliday.ReadOnly = true;
                    txtCarryImportPriceNight.ReadOnly = true;

                    txtCarryExportPrice.ReadOnly = true;
                    txtCarryExportPriceNight.ReadOnly = true;
                    txtCarryExportPriceHoliday.ReadOnly = true;

                    txtOtherPrice.ReadOnly = true;
                }
                else
                {
                    drChooseService.SelectedValue = dtContractDetail.Rows[0]["ServicesFOB_ID"].ToString();
                    txtCarryImportPrice.Text = String.Format("{0:0,00}",dtContractDetail.Rows[0]["CarryImportPrice"]).Replace(".", ",");
                    txtCarryImportPriceHoliday.Text = String.Format("{0:0,00}",dtContractDetail.Rows[0]["CarryImportPriceHoliday"]).Replace(".", ",");
                    txtCarryImportPriceNight.Text = String.Format("{0:0,00}",dtContractDetail.Rows[0]["CarryImportPriceNight"]).Replace(".", ",");

                    txtCarryExportPrice.Text = String.Format("{0:0,00}",dtContractDetail.Rows[0]["CarryExportPrice"]).Replace(".", ",");
                    txtCarryExportPriceNight.Text = String.Format("{0:0,00}",dtContractDetail.Rows[0]["CarryExportPriceNight"]).Replace(".", ",");
                    txtCarryExportPriceHoliday.Text = String.Format("{0:0,00}",dtContractDetail.Rows[0]["CarryExportPriceHoliday"]).Replace(".", ",");

                    txtOtherPrice.Text = String.Format("{0:0,00}", dtContractDetail.Rows[0]["OtherPrice"]).Replace(".", ",");

                    drChooseService.Enabled = false;
                    txtCarryImportPrice.ReadOnly = true;
                    txtCarryImportPriceHoliday.ReadOnly = true;
                    txtCarryImportPriceNight.ReadOnly = true;

                    txtCarryExportPrice.ReadOnly = true;
                    txtCarryExportPriceNight.ReadOnly = true;
                    txtCarryExportPriceHoliday.ReadOnly = true;

                    txtOtherPrice.ReadOnly = true;
                }
            }
        }
        protected void BindGridVehicleDetail(int id)
        {
            dtVehicleDetail = clsVehicle.BindGirdTMVehicle_Detail_Edit(id);
            if (dtVehicleDetail != null && dtVehicleDetail.Rows.Count > 0)
            {
                lbSoXe.Text = dtVehicleDetail.Rows[0]["Type_Number_Of_Vehicles"].ToString();
            }
        }
        protected void BindGridDropdownlist()
        {
            dtContracType = clsContractType.BindGirdTMContract_Type();
            dtCustomer = clsCustomer.BindGirdTMCustomers();
            dtVehicle = clsVehicle.BindGirdTMVehicle();
            _dtDichVuBocXep = _clsServices.BindGirdTMServicesFOB();
            if (dtContracType != null && dtContracType.Rows.Count > 0)
            {
                drChonLoaiHopDong.DataSource = dtContracType;
                drChonLoaiHopDong.DataTextField = "Type_Of_Contract_Name";
                drChonLoaiHopDong.DataValueField = "Type_Of_Contract_ID";
                drChonLoaiHopDong.DataBind();
            }
            if (dtCustomer != null && dtCustomer.Rows.Count > 0)
            {
                drKhachHang.DataSource = dtCustomer;
                drKhachHang.DataTextField = "Customers_Name";
                drKhachHang.DataValueField = "Customers_ID";
                drKhachHang.DataBind();
            }
            if (dtVehicle != null && dtVehicle.Rows.Count > 0)
            {
                drSoXe.DataSource = dtVehicle;
                drSoXe.DataTextField = "Vehicle_Number";
                drSoXe.DataValueField = "Vehicle_ID";
                drSoXe.DataBind();
            }
            if (_dtDichVuBocXep != null && dtAutoComplete.Rows.Count > 0)
            {
                drChooseService.DataSource = _dtDichVuBocXep;
                drChooseService.DataTextField = "ServicesFOB_Name";
                drChooseService.DataValueField = "ServicesFOB_ID";
                drChooseService.DataBind();
            }
        }
        protected void BindGridService(int id)
        {
            _dtDichVuBocXep = clsContract.BindGirdTMContract_Detail_Edit(id);
            if (_dtDichVuBocXep != null && _dtDichVuBocXep.Rows.Count > 0)
            {
                if (_dtDichVuBocXep.Rows[0]["ServicesFOB_ID"] == DBNull.Value || _dtDichVuBocXep.Rows[0]["ServicesFOB_ID"].ToString() == "0")
                {
                    drChooseService.Items.Clear();
                    drChooseService.Items.Add(new ListItem("--Chọn dịch vụ--", "-1"));
                    txtCarryImportPrice.Text = "0";
                    txtCarryImportPriceHoliday.Text = "0";
                    txtCarryImportPriceNight.Text = "0";

                    txtCarryExportPrice.Text = "0";
                    txtCarryExportPriceNight.Text = "0";
                    txtCarryExportPriceHoliday.Text = "0";

                    txtOtherPrice.Text = "0";

                    drChooseService.Enabled = false;
                    txtCarryImportPrice.ReadOnly = true;
                    txtCarryImportPriceHoliday.ReadOnly = true;
                    txtCarryImportPriceNight.ReadOnly = true;

                    txtCarryExportPrice.ReadOnly = true;
                    txtCarryExportPriceNight.ReadOnly = true;
                    txtCarryExportPriceHoliday.ReadOnly = true;

                    txtOtherPrice.ReadOnly = true;
                }
                else
                {
                    drChooseService.SelectedValue = _dtDichVuBocXep.Rows[0]["ServicesFOB_ID"].ToString();
                    txtCarryImportPrice.Text = _dtDichVuBocXep.Rows[0]["CarryImportPrice"].ToString();
                    txtCarryImportPriceHoliday.Text = _dtDichVuBocXep.Rows[0]["CarryImportPriceHoliday"].ToString();
                    txtCarryImportPriceNight.Text = _dtDichVuBocXep.Rows[0]["CarryImportPriceNight"].ToString();

                    txtCarryExportPrice.Text = _dtDichVuBocXep.Rows[0]["CarryExportPrice"].ToString();
                    txtCarryExportPriceNight.Text = _dtDichVuBocXep.Rows[0]["CarryExportPriceNight"].ToString();
                    txtCarryExportPriceHoliday.Text = _dtDichVuBocXep.Rows[0]["CarryExportPriceHoliday"].ToString();

                    txtOtherPrice.Text = _dtDichVuBocXep.Rows[0]["OtherPrice"].ToString();

                    drChooseService.Enabled = false;
                    txtCarryImportPrice.ReadOnly = true;
                    txtCarryImportPriceHoliday.ReadOnly = true;
                    txtCarryImportPriceNight.ReadOnly = true;

                    txtCarryExportPrice.ReadOnly = true;
                    txtCarryExportPriceNight.ReadOnly = true;
                    txtCarryExportPriceHoliday.ReadOnly = true;

                    txtOtherPrice.ReadOnly = true;
                }
            }
        }
        protected void Insert()
        {
            //Insert
            if (flag)
            {
                InsertContract();
            }
            else//Update
            {
                UpdateContract();
            }
        }
        protected bool CheckInsert()
        {
            bool kiemtra = false;
            dtContract = clsContract.BindGirdTMContract();
            if (dtContract != null && dtContract.Rows.Count > 0)
            {
                foreach (DataRow dr in dtContract.Rows)
                {
                    if (txtMaHopDong.Text == dr["Contrac_Alias"].ToString())
                    {
                        kiemtra = true;
                    }
                }
            }
            return kiemtra;
        }
        protected void InsertContract()
        {
            try
            {
                if (CheckInsert())
                {
                    lbThongBaoLoi.Text = "Mã hợp đồng này đã tồn tại !";
                    OpenDialog("#thongbao");
                    updatePanelThongBao.Update();
                }
                else
                {
                    if (lbSoXe.Text == "0")
                    {
                        lbThongBaoLoi.Text = "Hiện tại loại xe này đã hết.Vui lòng liên hệ sau !";
                        OpenDialog("#thongbao");
                        updatePanelThongBao.Update();
                    }
                    else
                    {
                        HttpCookie cookie = Request.Cookies["UserLogin"];
                        VanTaiProperty.clsTMContractProperty obj = new clsTMContractProperty();
                        obj.Contrac_Alias = txtMaHopDong.Text;
                        obj.Type_Of_Contract_ID = int.Parse(drChonLoaiHopDong.SelectedValue.ToString());
                        obj.Customers_ID = int.Parse(drKhachHang.SelectedValue.ToString());
                        obj.Vehicle_ID = int.Parse(drSoXe.SelectedValue.ToString());
                        obj.Member_ID = int.Parse(cookie["Users_ID"].ToString());
                        obj.Contract_Signed_Date = DateTime.Parse(txtNgayKyHopDong.Text);
                        obj.Contract_Expires_Date = DateTime.Parse(txtNgayHetHan.Text);
                        obj.Contract_Payment = float.Parse(txtTienDatCoc.Text.Replace(",", ""));
                        obj.Contract_Promotions = 0;
                        obj.Contract_Content = txtNoiDungHopDong.Text;
                        obj.Contract_Note = txtGhiChu.Text;
                        VanTaiBLL.clsTMContractBusiness.InsertTMContract(obj);
                        BindGridContract();
                        ClearTextBox();
                        lbThongBaoLoi.Text = "Thêm mẫu tin thành công !";
                        OpenDialog("#thongbao");
                        updatePanelThongBao.Update();
                    }
                }
            }
            catch (Exception ex)
            {
                lbThongBaoLoi.Text = ex.ToString();
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
        }
        protected void UpdateContract()
        {
            try
            {
                HttpCookie cookie = Request.Cookies["UserLogin"];
                VanTaiProperty.clsTMContractProperty obj = new clsTMContractProperty();
                obj.Contract_ID = int.Parse(lbID.Text);
                obj.Contrac_Alias = txtMaHopDong.Text;
                obj.Type_Of_Contract_ID = int.Parse(drChonLoaiHopDong.SelectedValue.ToString());
                obj.Customers_ID = int.Parse(drKhachHang.SelectedValue.ToString());
                obj.Vehicle_ID = int.Parse(drSoXe.SelectedValue.ToString());
                obj.Member_ID = int.Parse(cookie["Users_ID"].ToString());
                obj.Contract_Signed_Date = DateTime.Parse(txtNgayKyHopDong.Text);
                obj.Contract_Expires_Date = DateTime.Parse(txtNgayHetHan.Text);
                obj.Contract_Payment = float.Parse(txtTienDatCoc.Text.Replace(",", ""));
                obj.Contract_Promotions = 0;
                obj.Contract_Content = txtNoiDungHopDong.Text;
                obj.Contract_Note = txtGhiChu.Text;
                VanTaiBLL.clsTMContractBusiness.UpdateTMContract(obj);
                BindGridContract();
                lbThongBaoLoi.Text = "Cập nhật mẫu tin thành công !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            catch (Exception ex)
            {
                lbThongBaoLoi.Text = ex.ToString();
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
        }
        protected void Autocomplate()
        {
            dtAutoComplete = clsContract.BindGirdTMContract();
            if (dtAutoComplete != null && dtAutoComplete.Rows.Count > 0)
            {
                foreach (DataRow dr in dtAutoComplete.Rows)
                {
                    if (string.IsNullOrEmpty(SuggestionList))
                    {
                        SuggestionList += "\"" + dr["FullNameMem"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["Customers_Alias"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["Member_Alias"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["Customers_Name"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["Vehicle_Number"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["Contrac_Alias"].ToString().Replace("\n", " ") + "\",";
                    }
                    else
                    {
                        SuggestionList += "\"" + dr["FullNameMem"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["Customers_Alias"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["Member_Alias"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["Customers_Name"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["Vehicle_Number"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["Contrac_Alias"].ToString().Replace("\n", " ") + "\",";
                    }
                }
            }
        }
        protected void Search(string keywords)
        {
            dtSearch = clsContract.Search(keywords);
            if (dtSearch != null && dtSearch.Rows.Count > 0)
            {
                ListViewAll.DataSource = dtSearch;
                ListViewAll.DataBind();
                ListViewAll.Visible = true;
                DataPagerListAll.Visible = true;

                StrAlert += "<div class='alert alert-success'>";
                StrAlert += "<a href='#' class='close' data-dismiss='alert'>&times;</a>";
                StrAlert += "Tìm thấy " + dtSearch.Rows.Count.ToString() + " mẫu tin !";
                StrAlert += "</div>";
            }
            else
            {
                ListViewAll.Visible = false;
                DataPagerListAll.Visible = false;
                StrAlert += "<div class='alert alert-error'>";
                StrAlert += "<a href='#' class='close' data-dismiss='alert'>&times;</a>";
                StrAlert += "Không tìm thấy mẫu tin nào !";
                StrAlert += "</div>";
            }
        }
        protected void Fillter(string DateBegin, string EndBegin)
        {
            dtFilter = clsContract.TMContract_Fillter_Date(txtTuNgayNgayNhapKho.Text, txtDenNgayNhapKho.Text);
            if (dtFilter != null && dtFilter.Rows.Count > 0)
            {
                ListViewAll.DataSource = dtFilter;
                ListViewAll.DataBind();

                ListViewAll.Visible = true;
                DataPagerListAll.Visible = true;

                StrAlert += "<div class='alert alert-success'>";
                StrAlert += "<a href='#' class='close' data-dismiss='alert'>&times;</a>";
                StrAlert += "Tìm thấy " + dtFilter.Rows.Count.ToString() + " mẫu tin !";
                StrAlert += "</div>";
            }
            else
            {
                StrAlert += "<div class='alert alert-error'>";
                StrAlert += "<a href='#' class='close' data-dismiss='alert'>&times;</a>";
                StrAlert += "Không tìm thấy mẫu tin nào !";
                StrAlert += "</div>";
                ListViewAll.Visible = false;
                DataPagerListAll.Visible = false;
            }
        }
        #endregion

        #region[OpenDialog]
        private void OpenDialog(string dialogId)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('open');");
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        private void CloseDialog(string dialogId)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('close');");
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        #endregion

        #region[ClearTextbox]
        protected void ClearTextBox()
        {
            txtMaHopDong.Text = "";
            txtNgayKyHopDong.Text = DateTime.Now.ToString();
            txtNoiDungHopDong.Text = "";
            txtGhiChu.Text = "";
            txtTienDatCoc.Text = "0";

            txtCarryImportPrice.Text = "0";
            txtCarryImportPriceHoliday.Text = "0";
            txtCarryImportPriceNight.Text = "0";

            txtCarryExportPrice.Text = "0";
            txtCarryExportPriceNight.Text = "0";
            txtCarryImportPriceHoliday.Text = "0";

            txtOtherPrice.Text = "0";
            drChonLoaiHopDong.SelectedIndex = -1;
            drKhachHang.SelectedIndex = -1;
            drSoXe.SelectedIndex = -1;
        }
        #endregion

        #region[Permisstion]
        protected void CheckPermisstion()
        {
            HttpCookie cookie = Request.Cookies["UserLogin"];
            if (cookie != null)
            {
                bool Is_Page_All = SecurityServices.HasPermisstionPage(StringContent.QuanLyHopDong, int.Parse(cookie["RolesID"]), true);
                if (Is_Page_All == true)
                {
                    bool AddNew = SecurityServices.HasPermisstion(StringContent.QuanLyHopDong, int.Parse(cookie["RolesID"]), TypeaAudit.AddNew);
                    if (AddNew == false)
                    {
                        cmdAddNew.Visible = false;
                    }
                    else
                    {
                        cmdAddNew.Visible = true;
                    }
                    bool Edit = SecurityServices.HasPermisstion(StringContent.QuanLyHopDong, int.Parse(cookie["RolesID"]), TypeaAudit.Edit);
                    if (Edit == false)
                    {
                        cmdEdit.Visible = false;
                    }
                    else
                    {
                        cmdEdit.Visible = true;
                    }
                    bool Del = SecurityServices.HasPermisstion(StringContent.QuanLyHopDong, int.Parse(cookie["RolesID"]), TypeaAudit.Delete);
                    if (Del == false)
                    {
                        cmdDel.Visible = false;
                    }
                    else
                    {
                        cmdDel.Visible = true;
                    }
                }
                else
                {
                    Response.Redirect("error-page.aspx");
                }
            }
        }
        #endregion
    }
}